import pymongo
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from pymongo import MongoClient
from whoosh.lang.morph_en import exceptions

# 连接到MongoDB，默认连接到localhost的27017端口
client = MongoClient('mongodb://erp:erp123@localhost:27017', maxPoolSize=10,
                     minPoolSize=3,
                     maxConnecting=30,
                     socketTimeoutMS=100000,
                     maxIdleTimeMS=60000,
                     connectTimeoutMS=40000,
                     waitQueueTimeoutMS=10000)

db = client['erp']
collection = db['zhongyaocai_letter']

class ZhongyaocaiPipeline:
    def process_item(self, item, spider):

        # os.chdir('E:/Ana project/east_money/result_txt')
        # with open(item['title']+ '.txt', 'a', encoding='utf-8') as f:
        #     f.write('新闻链接：'+item['href']+'\n')
        #     if item['summary']:
        #         f.write(item['summary']+ '\n')
        #     else:
        #         f.write('此新闻没有摘要' + '\n')
        #     f.write(item['content'] + '\n')
        try:

            pass
            # collection.insert_one({"url":item["url"],"title":item["title"],"letter":item["letter"]})
        except Exception as e:
            print(f"app error:{e}")

        return item
